package com.ciaojian.worker.controller;

import com.ciaojian.core.base.annotation.ApiIdempotent;
import com.ciaojian.core.model.ProductGroup;
import com.ciaojian.core.util.Result;
import com.ciaojian.worker.model.param.UserParam;
import com.ciaojian.worker.service.CouponCodeServiceImpl;
import com.ciaojian.worker.service.ProductGroupServiceImpl;
import com.ciaojian.worker.service.UserServiceImpl;
import com.ciaojian.worker.service.WorkerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author oxiny
 */
@Api(tags = "用户模块: 用户相关")
@Validated
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserServiceImpl userService;
    @Resource
    private ProductGroupServiceImpl productGroupService;
    @Resource
    private CouponCodeServiceImpl couponCodeService;
    @Resource
    private WorkerServiceImpl workerService;

    @ApiOperation("用户权益券")
    @GetMapping("/member-detail/coupon")
    public Result getUserCoupon(@NotNull(message = "请选择用户") Integer userId) {
        return Result.ok(couponCodeService.getUserCouponCode(userId));
    }

    @ApiOperation("查找用户")
    @GetMapping
    public Result getUser(UserParam userParam) {
        return Result.ok(userService.getUser(userParam));
    }

    @ApiIdempotent
    @ApiOperation("给用户发放产品")
    @PostMapping("/give-product")
    public Result giveProduct(@NotNull(message = "请选择用户") Integer userId,
                              @NotNull(message = "请选择产品") Integer productId,
                              @NotNull(message = "发型师不能为空") Integer workerId) {
        // 发型师所属门店ID
        Integer storeId = workerService.getBaseMapper().getStoreIdByWorkerId(workerId);
        // 产品
        ProductGroup productGroup = new ProductGroup().selectById(productId);
        productGroupService.giveProduct(userId, workerId, storeId, productGroup);
        return Result.ok();
    }
}
